package com.ruoyi.caiwu.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.ruoyi.caiwu.service.CwPaymentPlanService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@RestController
@CrossOrigin
public class ZfPaycontroller {

    private final String CHARSET = "UTF-8";
    private final String SIGN_TYPE = "RSA2";
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqAb++gqjodEV8X9UR3Gs5QWWQ0tdRLf4biduvjpjeX+ho1nadEiDAZGoL3iEV8oC58IPXYtFpgbaxLkYjwsWSkQKOXknedE3a23QHVBzmONWS0bprFW0RSENXdqf1FsBHiFCNquCbxcKneIxmvK7sR7eP98nvLPfrLMc68U70KNR1nhWrBtzl8ehkhWyrZV4n3F/NnrmYkIs3XoSQmRq+M0Hfai7uP72m5boXB30E+37c6UUGCfROkgS5GQ0dcp1/Kct3fOO/+9iWvLKxJoAGaP1wBUTRvRAD9aWwFYhusaLQhqUwu7Y0iOJe9Z8aEhjJoDzTKVRjCDteFO2b6zpRQIDAQAB";
    @Autowired
    CwPaymentPlanService cwPaymentPlanService;



    @RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
    public String returnUrl(HttpServletRequest request, HttpServletResponse response)
            throws IOException, AlipayApiException {
        System.out.println("=================================同步回调=====================================");

        // 获取支付宝GET过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
            params.put(name, valueStr);
        }

        System.out.println(params);//查看参数都有哪些
        boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE); // 调用SDK验证签名
        //验证签名通过
        if(signVerified){
            // 商户订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");

            // 付款金额
            String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");


            System.out.println("商户订单号="+out_trade_no);
            System.out.println("支付宝交易号="+trade_no);
            System.out.println("付款金额="+total_amount);

            //支付成功，修复支付状态
            /*payService.updateById(Integer.valueOf(out_trade_no));*/
            String[] split = out_trade_no.split("\\|");
            cwPaymentPlanService.updatePayMent(split[0],split[1]);
            response.sendRedirect("http://localhost/caiwu/caiwu/fkjh/index");
            return "ok";//跳转付款成功页面http://localhost/caiwu/caiwu/fkjh/index
        }else{
            return "no";//跳转付款失败页面
        }

    }
}
